package com.ict.ms.trackPlayBack.facade.redis.init;

import redis.clients.jedis.Jedis;

import java.util.List;
import java.util.Random;

/**
 * Created by chenlong on 2016/11/10.
 */

/**
 * redis初始化，插入指定条数据
 */
public class initRedis {
    public static final String redisAddress="127.0.0.1";
    public static final int redisPort=6379;

//    public static final String redisAddress="122.5.51.230";
//    public static final int redisPort=10007;

    //    public static final String redisChat="JRedisChat";

    public static void main(String[] args){

        Jedis jedis=new Jedis(redisAddress,redisPort,999999);

        initMD_ALL_NUM(jedis,10);
        initOD_ALL_NUM(jedis,10);
//        for(int i=0;i<10;i++){
//            TestInsertRedis("test"+i,jedis,1000000);
//        }
    }

    /**
     * @param length 插入多少条测试数据
     */
    private static void initMD_ALL_NUM(Jedis jedis,int length){
        String key_MD_ALL_NUM="MD_ALL_KEY";
        String key_MD_NUM="";

        for(int i=0;i<length;i++){
            //列表
            jedis.lpush(key_MD_ALL_NUM,""+4124+i);
            //有序集合
        }

        List<String> list = jedis.lrange(key_MD_ALL_NUM, 0 ,999999);
        System.out.println(list.size());

        int k=10;
        for(int i=0; i<list.size(); i++) {
            System.out.println("initRedis List in redis:: "+list.get(i));

            key_MD_NUM="MDV"+4124+i;
            for(int j=0;j<k;j++){
                jedis.zadd(key_MD_NUM,1478767902+j,1478767902+j+"~1~72932609~16940444~290~29~23~412345678");
            }
            System.out.println(key_MD_NUM);
            for(int j=0;j<k;j++){
                System.out.println(key_MD_NUM+" sorted in redis::"+jedis.zrangeByScore(key_MD_NUM,1478767902,1478767902+k));
            }
        }
    }

    private static void initOD_ALL_NUM(Jedis jedis,int length){
        String key_OD_ALL_NUM="OD_ALL_KEY";
        String key_MD_NUM="";
        String key_OD_NUM="";

        for(int i=0;i<length;i++){
            //列表
            jedis.lpush(key_OD_ALL_NUM,""+4124+i);
            //有序集合
        }

        List<String> list = jedis.lrange(key_OD_ALL_NUM, 0 ,999999);
        System.out.println(list.size());

        int k=10;
        for(int i=0; i<list.size(); i++) {
            System.out.println("OD_ALL_NUM List in redis:: "+list.get(i));

            key_MD_NUM="ODV"+5124+i;
            for(int j=0;j<k;j++){
                jedis.zadd(key_MD_NUM,1478767902+j,1478767902+j+"~1~72932609~16940444~290~29~23~412345678");
            }
            System.out.println(key_MD_NUM);
            for(int j=0;j<k;j++){
                System.out.println(key_OD_NUM+" sorted in redis::"+jedis.zrangeByScore(key_MD_NUM,1478767902,1478767902+k));
            }
        }
    }

    private static int getRandomType(){
        Random random=new Random();
        int type=random.nextInt(4);
        if(type==0){
            return 1;
        }else{
            return type;
        }
    }

    private static void TestInsertRedis(String key,Jedis jedis,int length){
        long startTime=System.currentTimeMillis()/1000;
        System.out.println("起始时间："+startTime);

        for(int i=0;i<length;i++){
            jedis.zadd(key,i,i+"~1~72932609~16940444~290~29~23~412345678~"+i);
        }
        long endTime=System.currentTimeMillis()/1000;
        System.out.println("结束时间："+endTime);
        long useTime=endTime-startTime;
        System.out.println("向redis插入"+length+"条数据用时："+useTime);
    }
}